package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import stuff.DialogDisposeAction;
import stuff.FixedSizeDocument;

public class GUIIPConfig extends GUIDialog {
	private static final long serialVersionUID = 1L;
	private JTextField tfPort, tfIP;
	private JButton btOK,btCancel;
	private JLabel loginResult;
	private JFrame param;
	private String port, ip;
	
	public GUIIPConfig(JFrame param) {
		super(param);
		this.param = param;
		setExitAction(new DialogDisposeAction(this, param));
		setTitle("Thiết lập IP máy chủ");
		
		JPanel ipConfirm = new JPanel(new GridBagLayout());
		ipConfirm.setOpaque(false);
		
		JLabel lpPort, lbIP;
		lpPort = new JLabel();
		lpPort.setText("Port :  ");
		lpPort.setFont(lpPort.getFont().deriveFont(16f));
		lpPort.setForeground(new Color(40,40,40));
		
		lbIP = new JLabel();
		lbIP.setText("IP :");
		lbIP.setFont(lbIP.getFont().deriveFont(16f));
		lbIP.setForeground(new Color(40,40,40));
		
		loginResult = new JLabel("");
		loginResult.setFont(lbIP.getFont().deriveFont(12f));
		loginResult.setForeground(Color.red);
		
		tfPort = new JTextField();
		tfPort.setDocument(new FixedSizeDocument(20));
		tfPort.setPreferredSize(new Dimension(0, tfPort.getPreferredSize().height +6));
		tfPort.setFont(tfPort.getFont().deriveFont(16f));
		tfPort.setForeground(new Color(40,40,40));
		
		
		tfIP = new JTextField();
		tfIP.setDocument(new FixedSizeDocument(20));
		tfIP.setPreferredSize(new Dimension(0, tfIP.getPreferredSize().height +6));
		tfIP.setFont(tfIP.getFont().deriveFont(16f));
		tfIP.setForeground(new Color(40,40,40));
		
		GridBagConstraints c= new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0;
		ipConfirm.add(lbIP,c);
		c.gridx = 1;
		c.weightx = 0.7f;
		ipConfirm.add(tfIP,c);
		
		c.gridx = 1;
		c.gridy = 1;
		ipConfirm.add(Box.createVerticalStrut(6),c);
		c.gridx = 0;
		c.gridy = 2;
		c.weightx = 0;
		ipConfirm.add(lpPort,c);
		c.gridx = 1;
		c.weightx = 0;
		ipConfirm.add(tfPort,c);
		
		c.gridx = 1;
		c.gridy = 3;
		ipConfirm.add(Box.createVerticalStrut(6),c);
		
		c.gridx = 0;
		c.gridy = 4;
		c.weightx = 0;
		c.gridwidth = 2;
		ipConfirm.add(loginResult,c);
		
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		ipConfirm.setOpaque(true);
		
		btOK = new CustomJButton("Xác nhận", 110, 30, Color.WHITE, CustomJButton.DARKER, 
				Color.DARK_GRAY, 2, false, true);
		btOK.addActionListener(new ConfigAction());
		
		btCancel = new CustomJButton("Thoát", 90, 30, Color.WHITE, CustomJButton.DARKER, 
				Color.DARK_GRAY, 2, false, true);
		btCancel.addActionListener(new DialogDisposeAction(this, param));
		
		buttonPanel.add(btOK);
		buttonPanel.add(btCancel);
		
		JPanel displayPanel = new JPanel(new BorderLayout());
		displayPanel.setOpaque(false);
		displayPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
		displayPanel.add(ipConfirm,BorderLayout.CENTER);
		displayPanel.add(buttonPanel,BorderLayout.SOUTH);
		
		this.setContentLayout(displayPanel);
		getIP();
	}
	
	public void getIP(){
		File ipFile = new File("ipConfig.dbz");
		if(ipFile.exists()){
			try {
				DataInputStream is = new DataInputStream(new FileInputStream(ipFile));
				port = is.readUTF();
				ip = is.readUTF();
				is.close();
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, "Có lỗi khi đọc tập tin ipConfig.dbz",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				port = "27000";
				ip = "";
			}	
		}
		else{
			JOptionPane.showMessageDialog(this, "Không đúng cấu hình ip máy chủ .",
					"Thông báo", JOptionPane.WARNING_MESSAGE);
			port = "27000";
			ip = "";
		}
		tfPort.setText(port);
		tfIP.setText(ip);
	}
	
	class ConfigAction extends AbstractAction{
		
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			if(tfPort.getText().trim().equals("")){
				loginResult.setText("Port không được để trống.");
				return;
			}
			if(tfIP.getText().trim().equals("")){
				loginResult.setText("IP không được để trống.");
				return;
			}
			
			try {
				Integer.parseInt(tfPort.getText());
				DataOutputStream os = new DataOutputStream(new FileOutputStream("ipConfig.dbz"));
				os.writeUTF(tfPort.getText().trim());
				os.writeUTF(tfIP.getText().trim());
				os.flush();
				os.close();
				JOptionPane.showMessageDialog(GUIIPConfig.this, "Thiết lập IP máy chủ thành công.",
						"Thông báo", JOptionPane.INFORMATION_MESSAGE);
				param.setEnabled(true);
				param.requestFocus();
				dispose();
			} 
			catch (IOException ea) {
				loginResult.setText("Thiết lập IP máy chủ thành công.");
			} catch (NumberFormatException ea){
				loginResult.setText("Port chỉ có thể nhận được số.");
			}
		}
	}
}